package reflectdemo;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class ReflectClassDemo {

    public static void reflectNewInstance() {
        Class<?> stu1;
        try {
            stu1 = Class.forName("reflectdemo.Student");
            Student student = (Student)stu1.newInstance();
            System.out.println(student);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }


    public static void reflectPrivateConstructor() {
        Class<?> stu1;
        try {
            stu1 = Class.forName("reflectdemo.Student");
            Constructor<?> constructor =  stu1.getDeclaredConstructor(String.class,int.class);

            constructor.setAccessible(true);

            Student student = (Student)constructor.newInstance("zhangsan",99);

            System.out.println(student);

        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }


    public static void reflectPrivateField() {
        Class<?> stu1;
        try {
            stu1 = Class.forName("reflectdemo.Student");

            Field field = stu1.getDeclaredField("name");
            field.setAccessible(true);

            Student student = (Student)stu1.newInstance();
            //1.修改哪个对象的字段  2.修改的值是什么
            field.set(student,"lisi");

            System.out.println(student);

        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (NoSuchFieldException e) {
            throw new RuntimeException(e);
        }
    }

    public static void reflectPrivateMethod() {
        Class<?> stu1;
        try {
            stu1 = Class.forName("reflectdemo.Student");

            Method method = stu1.getDeclaredMethod("function",String.class);

            Student student = (Student)stu1.newInstance();

            method.setAccessible(true);

            method.invoke(student,"我试试传一个参数");

        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }
    }




    public static void main(String[] args) {
        //reflectNewInstance();
        //reflectPrivateConstructor();
        //reflectPrivateField();

        reflectPrivateMethod();
    }
}
